package com.wanli.agent.homepage;

import android.os.Bundle;
import android.text.InputFilter;
import android.widget.EditText;
import android.widget.TextView;

import com.wanli.agent.R;
import com.wanli.agent.base.BaseActivity;
import com.wanli.agent.base.DataCallBack;
import com.wanli.agent.bean.BaseResponseBean;
import com.wanli.agent.bean.RateNewBean;
import com.wanli.agent.homepage.model.HomePageModelImpl;
import com.wanli.agent.homepage.model.IHomePageModel;
import com.wanli.agent.utils.CashierInputFilterThree;
import com.wanli.agent.utils.SPManager;
import com.wanli.agent.utils.ToastUtil;
import com.wanli.agent.widget.AppTitleBar;
import com.wanli.agent.widget.ViewLoading;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SettlementRulesActivity extends BaseActivity {

    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.tv_debit_card_name)
    TextView tvDebitCardName;
    @BindView(R.id.edit_debit_card_value)
    EditText editDebitCardValue;
    @BindView(R.id.tv_quickpass_debit_name)
    TextView tvQuickpassDebitName;
    @BindView(R.id.edit_quickpass_debit_value)
    EditText editQuickpassDebitValue;
    @BindView(R.id.tv_debit_card_cap_name)
    TextView tvDebitCardCapName;
    @BindView(R.id.edit_debit_card_cap_value)
    EditText editDebitCardCapValue;
    @BindView(R.id.tv_credit_card_name)
    TextView tvCreditCardName;
    @BindView(R.id.edit_credit_card_value)
    EditText editCreditCardValue;
    @BindView(R.id.tv_quickpass_credit_name)
    TextView tvQuickpassCreditName;
    @BindView(R.id.edit_quickpass_credit_value)
    EditText editQuickpassCreditValue;
    @BindView(R.id.tv_wechat_alipay_name)
    TextView tvWechatAlipayName;
    @BindView(R.id.edit_wechat_alipay_value)
    EditText editWechatAlipayValue;
    @BindView(R.id.tv_wechat_alipay_d0_name)
    TextView tvWechatAlipayD0Name;
    @BindView(R.id.edit_wechat_alipay_d0_value)
    EditText editWechatAlipayD0Value;
    @BindView(R.id.tv_waiver_name)
    TextView tvWaiverName;
    @BindView(R.id.edit_waiver_value)
    EditText editWaiverValue;
    @BindView(R.id.tv_huabei_name)
    TextView tvHuabeiName;
    @BindView(R.id.edit_huabei_value)
    EditText editHuabeiValue;

    @BindView(R.id.tv_small_money)
    TextView tv_small_money;
    @BindView(R.id.edit_small_money)
    EditText edit_small_money;

    @BindView(R.id.tv_unit_money)
    TextView tv_unit_money;
    @BindView(R.id.edit_unit_money)
    EditText edit_unit_money;

    @BindView(R.id.tv_firm_money)
    TextView tv_firm_money;
    @BindView(R.id.edit_firm_money)
    EditText edit_firm_money;


    @BindView(R.id.tv_commit)
    TextView tvCommit;
    private String agent_id;
    private IHomePageModel homePageModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settlement_rules);
        ButterKnife.bind(this);
        initView();
    }

    public void initView() {
        agent_id = getIntent().getStringExtra("agent_id");
        homePageModel = new HomePageModelImpl();
        requestShareMoneyRateNewMy();
        requestShareMoneyRateNew();

        String rate = SPManager.getInstance().getRate();

        editHuabeiValue.setHint(rate+"～0.6");

        InputFilter[] filters = {new CashierInputFilterThree()};
        editWaiverValue.setFilters(filters);
        editWechatAlipayValue.setFilters(filters);
        editQuickpassDebitValue.setFilters(filters);
        editQuickpassCreditValue.setFilters(filters);
        editDebitCardValue.setFilters(filters);
        editDebitCardCapValue.setFilters(filters);
        editCreditCardValue.setFilters(filters);
        editWechatAlipayD0Value.setFilters(filters);
        editHuabeiValue.setFilters(filters);
    }

    public void requestShareMoneyRateNewMy() {
        homePageModel.requestShareMoneyRateNewMy(new DataCallBack<RateNewBean>() {
            @Override
            public void onSuccessful(RateNewBean bean) {
                setRateNewMy(bean);
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    public void requestShareMoneyRateNew() {
        homePageModel.requestShareMoneyRateNew(agent_id, new DataCallBack<RateNewBean>() {
            @Override
            public void onSuccessful(RateNewBean bean) {
                setRateNew(bean);
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    /**
     * 'waiver' => 1,         //免手续费
     * 'wechat_alipay' => 2,  //微信支付宝
     * 'wechat_alipay_d0' => 8, //d0费用
     * <p>
     * 'quickpass_debit' => 3,  //借记卡优惠费率
     * 'quickpass_credit' => 4, //贷记卡(信用卡)优惠费率
     * <p>
     * 'debit_card' => 5,      //借记卡标准费率
     * 'debit_card_cap' => 6,  //借记卡标准费率封顶
     * 'credit_card' => 7 ,     //贷记卡(信用卡)标准费率
     * <p>
     * 'card_bonus'    =>  9,  //码牌奖励费率(80)
     * 'card_bonus_low'=>  10,  //码牌奖励费率
     */
    public void setRateNewMy(RateNewBean bean) {
        List<RateNewBean.DataBean> mList = bean.getData();
        for (RateNewBean.DataBean dataBean : mList) {
            switch (dataBean.getRule_type()) {
                case 1:
                    tvWaiverName.setText("免手续费(参考" + dataBean.getRate() + ")");
                    break;
                case 2:
                    tvWechatAlipayName.setText("标准费率(参考" + dataBean.getRate() + ")");
                    break;
                case 3:
                    tvQuickpassDebitName.setText("优惠费率(参考" + dataBean.getRate() + ")");
                    break;
                case 4:
                    tvQuickpassCreditName.setText("优惠费率(参考" + dataBean.getRate() + ")");
                    break;
                case 5:
                    tvDebitCardName.setText("标准费率(参考" + dataBean.getRate() + ")");
                    break;
                case 6:
                    tvDebitCardCapName.setText("标准费率封顶(参考" + dataBean.getRate() + ")");
                    break;
                case 7:
                    tvCreditCardName.setText("标准费率(参考" + dataBean.getRate() + ")");
                    break;
                case 8:
                    tvWechatAlipayD0Name.setText("D0费用(参考" + dataBean.getRate() + ")");
                    break;
                case 13:
                    tvHuabeiName.setText("微信/支付宝(参考" + dataBean.getRate() + ")");
                    break;
                case 14:
                    tv_small_money.setText("小微返现(参考" + dataBean.getRate() + ")");
                    break;
                case 15:
                    tv_unit_money.setText("个体返现(参考" + dataBean.getRate() + ")");
                    break;
                case 16:
                    tv_firm_money.setText("企业返现(参考" + dataBean.getRate() + ")");
                    break;
            }
        }
    }

    public void setRateNew(RateNewBean bean) {
        List<RateNewBean.DataBean> mList = bean.getData();
        for (RateNewBean.DataBean dataBean : mList) {
            switch (dataBean.getRule_type()) {
                case 1:
                    editWaiverValue.setText(dataBean.getRate());
                    editWaiverValue.setSelection(dataBean.getRate().length());
                    break;
                case 2:
                    editWechatAlipayValue.setText(dataBean.getRate());
                    editWechatAlipayValue.setSelection(dataBean.getRate().length());
                    break;
                case 3:
                    editQuickpassDebitValue.setText(dataBean.getRate());
                    editQuickpassDebitValue.setSelection(dataBean.getRate().length());
                    break;
                case 4:
                    editQuickpassCreditValue.setText(dataBean.getRate());
                    editQuickpassCreditValue.setSelection(dataBean.getRate().length());
                    break;
                case 5:
                    editDebitCardValue.setText(dataBean.getRate());
                    editDebitCardValue.setSelection(dataBean.getRate().length());
                    break;
                case 6:
                    editDebitCardCapValue.setText(dataBean.getRate());
                    editDebitCardCapValue.setSelection(dataBean.getRate().length());
                    break;
                case 7:
                    editCreditCardValue.setText(dataBean.getRate());
                    editCreditCardValue.setSelection(dataBean.getRate().length());
                    break;
                case 8:
                    editWechatAlipayD0Value.setText(dataBean.getRate());
                    editWechatAlipayD0Value.setSelection(dataBean.getRate().length());
                    break;
                case 13:
                    editHuabeiValue.setText(dataBean.getRate());
                    editHuabeiValue.setSelection(dataBean.getRate().length());
                    break;
                case 14:
                    edit_small_money.setText(dataBean.getRate());
                    edit_small_money.setSelection(dataBean.getRate().length());
                    break;
                case 15:
                    edit_unit_money.setText(dataBean.getRate());
                    edit_unit_money.setSelection(dataBean.getRate().length());
                    break;
                case 16:
                    edit_firm_money.setText(dataBean.getRate());
                    edit_firm_money.setSelection(dataBean.getRate().length());
                    break;

            }
        }
    }

    public void setShareMoneyRateNew() {
        List<RateNewBean.DataBean> mList = new ArrayList<>();
        for (int i = 1; i < 13; i++) {
            RateNewBean.DataBean dataBean = new RateNewBean.DataBean();
            dataBean.setRule_type(i);
            switch (i) {
                case 1:
                    dataBean.setRate(editWaiverValue.getText().toString());
                    break;
                case 2:
                    dataBean.setRate(editWechatAlipayValue.getText().toString());
                    break;
                case 3:
                    dataBean.setRate(editQuickpassDebitValue.getText().toString());
                    break;
                case 4:
                    dataBean.setRate(editQuickpassCreditValue.getText().toString());
                    break;
                case 5:
                    dataBean.setRate(editDebitCardValue.getText().toString());
                    break;
                case 6:
                    dataBean.setRate(editDebitCardCapValue.getText().toString());
                    break;
                case 7:
                    dataBean.setRate(editCreditCardValue.getText().toString());
                    break;
                case 8:
                    dataBean.setRate(editWechatAlipayD0Value.getText().toString());
                    break;
                case 9:
                    dataBean.setRule_type(13);
                    dataBean.setRate(editHuabeiValue.getText().toString());
                    break;
                case 10:
                    dataBean.setRule_type(14);
                    dataBean.setRate(edit_small_money.getText().toString());
                    break;
                case 11:
                    dataBean.setRule_type(15);
                    dataBean.setRate(edit_unit_money.getText().toString());
                    break;
                case 12:
                    dataBean.setRule_type(16);
                    dataBean.setRate(edit_firm_money.getText().toString());
                    break;
            }
            mList.add(dataBean);
        }
        ViewLoading.showProgress(mActivity, "提交中......");
        homePageModel.requestSetShareMoneyRateNew(agent_id, mList, new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {
                ToastUtil.showShort(bean.getMsg());
                finish();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }

    @OnClick(R.id.tv_commit)
    public void onViewClicked() {
        setShareMoneyRateNew();
    }
}
